package com.gyt.framework.response;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.List;

/**
 * @Author created by v_yantgao
 * @Date 2021/1/6 16:40
 */
@Data
@SuppressWarnings("serial")
@ApiModel(description = "分页对象")
public class Page<T> {

    @ApiModelProperty("分页对象数据")
    private List<T> list;

    @ApiModelProperty("分页对象数据总数")
    private long total;

    @ApiModelProperty("分页页码")
    private int pageNum;

    @ApiModelProperty("分页大小")
    private int pageSize;

    public static void startPage(int pageNum, int pageSize){
        PageHelper.startPage(pageNum, pageSize);
    }

    public static <T> Page<T> from(List<T> data){
        PageInfo<T> pageInfo = new PageInfo<>(data);
        Page<T> page = new Page<>();
        page.list = data;
        page.total = pageInfo.getTotal();
        page.pageNum = pageInfo.getPageNum();
        page.pageSize = pageInfo.getPageSize();
        return page;
    }
}